package com.poker.controller;

import com.poker.consts.Consts;
import com.poker.entity.SystemSetting;
import com.poker.service.SystemSettingService;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * @Author: yuanZ;
 * @Date: 2018/10/11 \;
 * @Description: 系统参数配置;
**/
@Controller
@RequestMapping("/systemSetting")
public class SystemSettingController extends BaseController {

    @Resource
    private SystemSettingService settingService;

    @RequestMapping("/list")
    public String list(Model model, SystemSetting setting,
                       @RequestParam(defaultValue = "0") Integer page,
                       @RequestParam(defaultValue = "10") Integer size) {
        Page<SystemSetting> data = settingService.findSystemSettingNoCriteria(page, size);

        model.addAttribute("data", data);
        model.addAttribute("setting", setting);
        return managerPage("systemSetting/list");
    }

    @RequestMapping("/findQuery")
    public String findQuery(Model model , SystemSetting setting,
                            @RequestParam(defaultValue = "0") Integer page,
                            @RequestParam(defaultValue = "10") Integer size) {
        Page<SystemSetting> data = settingService.findSystemSettingCriteria(page, size, setting);

        model.addAttribute("data", data);
        model.addAttribute("setting", setting);
        return managerPage("systemSetting/list");
    }

    @RequestMapping("/edit")
    public String edit(Model model, String id, String editType) {
        SystemSetting setting = null;

        if (Consts.EditType.EDIT.equals(editType)) setting = settingService.getById(id);

        model.addAttribute("setting", setting);
        model.addAttribute("editType", editType);
        return managerPage("systemSetting/edit");
    }

    @ResponseBody
    @RequestMapping("/doEdit")
    public void doEdit(SystemSetting setting) {
        settingService.updateKeyValue(setting.getValue(), setting.getId());
    }

}
